﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WHC.OrderWater.Contract;
using WHC.OrderWater.Business;
using WHC.Hotel.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmEditMessage : BaseForm
    {
        public string ID = string.Empty;

        public FrmEditMessage()
        {
            InitializeComponent();
        }

        private void FrmEditArea_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ID))
            {
                this.Text = "查看 " + this.Text;
                SystemMessageInfo info = BLLFactory<SystemMessage>.Instance.FindByID(ID);
                if (info != null)
                {
                    txtTitle.Text = info.Title;
                    txtContent.Text = info.Content;
                    lblCreateDate.Text = info.LastUpdated.ToString();
                }
            }
            else
            {
                this.Text = "新建 " + this.Text;
                lblCreateDate.Text = DateTime.Now.ToString();
            }
        }

        private void SetInfo(SystemMessageInfo info)
        {
            info.Title = txtTitle.Text;
            info.Content = txtContent.Text;
            info.LastUpdated = DateTime.Now;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ID))
            {
                SystemMessageInfo info = BLLFactory<SystemMessage>.Instance.FindByID(ID);
                if (info != null)
                {
                    SetInfo(info);

                    try
                    {
                        bool succeed = BLLFactory<SystemMessage>.Instance.Update(info, info.ID);
                        if (succeed)
                        {
                            MessageUtil.ShowTips("保存成功");
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                }
            }
            else
            {
                SystemMessageInfo info = new SystemMessageInfo();
                SetInfo(info);
                info.ID = Guid.NewGuid().ToString();

                try
                {
                    bool succeed = BLLFactory<SystemMessage>.Instance.Insert(info);
                    if (succeed)
                    {
                        MessageUtil.ShowTips("保存成功");
                        this.DialogResult = DialogResult.OK;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}
